package controllers;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;

import model.db.DataBase;
import model.dtos.Team;

import views.windows.MainFrame;

public class ApplicationController extends MainFrame
{

	private static final long serialVersionUID = 1L;

	public ApplicationController()
	{
		super();
		addActions();
	}
	
	public void addActions()
	{
		getSalirMenuItem().addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);				
				dispose();
			}
		});
		
		getRegistrarEquipoMenuItem().addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				
				getContentPane().removeAll();
				getContentPane().setLayout(new BorderLayout());
				getContentPane().add(new RegisterTeamController(), BorderLayout.CENTER);
				pack();
			}
		});
		
		getRegistrarUmpireMenuItem().addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e)
			{
				getContentPane().removeAll();
				getContentPane().setLayout(new BorderLayout());
				getContentPane().add(new RegisterUmpireController(), BorderLayout.CENTER);
				pack();
				
			}
		});
		
		getCargarDatosMenuItem().addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e)
			{
				getContentPane().removeAll();
				getContentPane().setLayout(new BorderLayout());
				getContentPane().add(new LoadDataController(), BorderLayout.CENTER);
				pack();
			}
		});
		
		getSimularMenuItem().addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e)
			{
				boolean ready = true;
				
				if (DataBase.getSeason().getTeams().size() == 0 || DataBase.getSeason().getUmpires().size() == 0)
					ready = false;
				
				for (Team t : DataBase.getSeason().getTeams())
				{
					if (t.getPlayers().size() == 0)
						ready = false;
				}
					
				if (!ready)
					showMessage("Liga no esta lista para ser simulada");
				else
				{
					DataBase.getSeason().playSeason();
					showMessage("Liga simulada con exito");
				}
			}
		});
		
		getCalendarioMenuItem().addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e)
			{
				getContentPane().removeAll();
				getContentPane().setLayout(new BorderLayout());
				getContentPane().add(new CalendarController(), BorderLayout.CENTER);
				pack();
			}
		});
		
		getPosicionesMenuItem().addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e)
			{
				getContentPane().removeAll();
				getContentPane().setLayout(new BorderLayout());
				getContentPane().add(new PositionTableController(), BorderLayout.CENTER);
				pack();
			}
		});
		
		getEstadisticaGeneralesMenuItem().addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e)
			{
				getContentPane().removeAll();
				getContentPane().setLayout(new BorderLayout());
				getContentPane().add(new StatisticsController(), BorderLayout.CENTER);
				pack();
			}
		});
	}
	
	public void startApplication()
	{
		setVisible(true);
	}
}
